package com.atguigu.exam.controller;


import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.jdbc.Null;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "轮播图管理", description = "轮播图相关操作，包括图片上传、轮播图增删改查、状态管理等功能")  // Swagger API分组
@RestController
@RequestMapping("/api/banners")
@CrossOrigin
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "获取所有轮播图", description = "获取所有轮播图列表，包括启用和禁用的，供管理后台使用")  // API描述
    @GetMapping("/list")
    public Result<List<Banner>> findAll() {
        List<Banner> bannerList = bannerService.list();
        return Result.success(bannerList, "获取轮播图列表成功");
    }


    @Operation(summary = "获取启用的轮播图")
    @GetMapping("/active")
    public Result getActiveBannerList() {
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装查询条件
        bannerLambdaQueryWrapper.eq(Banner::getIsActive, true);
        //调用BannerService中带条件查询的方法
        List<Banner> bannerList = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(bannerList, "获取启动的轮播图成功");
    }

    @Operation(summary = "根据ID获取轮播图")
    @GetMapping("/{id}")  // 处理GET请求
    public Result<Banner> getById(@PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        return Result.success(banner, "根据id查询轮播图成功");
    }


    @Operation(summary = "添加轮播图")
    @PostMapping("/add")
    public Result add(@RequestBody Banner banner) {
//调用BannerService中的save凡凡
        boolean save = bannerService.save(banner);
        if (save) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }


    @Operation(summary = "更新轮播图")
    @PutMapping("/update")
    public Result update(@RequestBody Banner banner) {
        //调用BannerService中的updateById方法
        boolean update = bannerService.updateById(banner);
        if (update) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }


    @Operation(summary = "根据id删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        //调用BannerService中的removeById方法
        boolean remove = bannerService.removeById(id);
        if (remove) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }


    @Operation(summary = "切换轮播图状态")
    @PutMapping("/toggle/{id}")
    public Result toggleActiveStatus(@PathVariable Long id,
                                     @RequestParam Boolean isActive) {

        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        bannerLambdaUpdateWrapper.set(Banner::getIsActive, isActive).eq(Banner::getId, id);
        boolean update = bannerService.update(bannerLambdaUpdateWrapper);
        if (update) {
            return Result.success("更新轮播图的状态成功");
        } else {
            return Result.error("更新轮播图的状态失败");
        }
    }


    @Operation(summary = "上传轮播图图片")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file) {
        //调用FileUploadService中上传文件的方法
        String fileUrl = fileUploadService.uploadFile(file);
        if (fileUrl == null) {
            return Result.error("上传轮播图图片失败");
        } else {
            return Result.success(fileUrl, "上传轮播图图片成功");
        }

    }
}
